/*
    XTU OJ: 1258
    by: fifth_light
*/

#include <stdio.h>
#include <assert.h>

void init_matrix(int matrix[10][10], int n) {
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < n; x++) {
            matrix[y][x] = y * n + x + 1;
        }
    }
}

void print_matrix(int matrix[10][10], int n) {
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < n; x++) {
            printf("%d", matrix[y][x]);
            if (x != n - 1 || y != n - 1) {
                putchar(' ');
            }
        }
    }
    putchar('\n');
}

void move_left(int matrix[10][10], int n, int mx, int my) {
    int orig[10];
    for (int i = 0; i < n; i++) {
        orig[i] = matrix[mx - 1][i];
    }
    for (int i = 0; i < n; i++) {
        int new_x = i + my;
        new_x %= n;
        matrix[mx - 1][i] = orig[new_x];
    }
}

void move_right(int matrix[10][10], int n, int mx, int my) {
    int orig[10];
    for (int i = 0; i < n; i++) {
        orig[i] = matrix[mx - 1][i];
    }
    for (int i = 0; i < n; i++) {
        int new_x = i - my;
        if (new_x < 0) {
            new_x += n;
        }
        matrix[mx - 1][i] = orig[new_x];
    }
}

void move_up(int matrix[10][10], int n, int mx, int my) {
    int orig[10];
    for (int i = 0; i < n; i++) {
        orig[i] = matrix[i][mx - 1];
    }
    for (int i = 0; i < n; i++) {
        int new_y = i + my;
        new_y %= n;
        matrix[i][mx - 1] = orig[new_y];
    }
}

void move_down(int matrix[10][10], int n, int mx, int my) {
    int orig[10];
    for (int i = 0; i < n; i++) {
        orig[i] = matrix[i][mx - 1];
    }
    for (int i = 0; i < n; i++) {
        int new_y = i - my;
        if (new_y < 0) {
            new_y += n;
        }
        matrix[i][mx - 1] = orig[new_y];
    }
}

int main(void) {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        int n, m;
        scanf("%d%d", &n, &m);
        int matrix[10][10];
        init_matrix(matrix, n);
        for (int j = 0; j < m; j++) {
            char c;
            int x, y;
            scanf(" %c%d%d", &c, &x, &y);
            switch (c) {
                case 'L':
                    move_left(matrix, n, x, y);
                    break;
                case 'R':
                    move_right(matrix, n, x, y);
                    break;
                case 'U':
                    move_up(matrix, n, x, y);
                    break;
                case 'D':
                    move_down(matrix, n, x, y);
                    break;
                default:
                    assert(0);
                    break;
            }
        }
        print_matrix(matrix, n);
    }
    return 0;
}
